<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\OrderService;
use App\Services\CenterService;
use App\Services\CommonService;
use Session;
use App\Services\MemberService;

class OrderController extends Controller{
    public function ModiOrderStatus($id,$mid){
        if($mid == 'null'){
            $_data = array('code'=>'3x000001','msg'=>'请登录','uri'=>'http://admin.boomdog.cn/');
        }else{
            $order = new OrderService;
            $data['composition_status'] = 0;
            $data['close_time'] = date('Y-m-d H:i:s');
            $data['isshow'] = 0;
            $data['update_date'] = date('Y-m-d H:i:s');

            $res = $order->updateOrder($id,$data);
            if($res){
                $_data = array('code'=>'2x000001','msg'=>'订单已取消');
            }else{
                $_data = array('code'=>'4x000001','msg'=>'取消订单失败');
            }
        }
        return response()->json($_data);
    }

    public function AddOrder(){
        if(!intval($_POST['member_id'])){
            $_data = array('code'=>'3x000001','msg'=>'请登录');
        }else{
            $member = new MemberService;
            $userinfo = $member->getMemberInfoByid(intval($_POST['member_id']));
            date_default_timezone_set('PRC');
            $order = new OrderService;
            $center = new CenterService;
            $default_address = $center->getDefaultAddress(intval($_POST['member_id']));

            $product_type = intval($_POST['product_type']);
            switch($product_type){
                case 1:
                    $prefix = 'Course';
                    break;
                case 2:
                    $prefix = 'Realia';
                    break;
                case 3:
                    $prefix = 'Realias';
                    break;
                case 4:
                    $prefix = 'Activity';
                    break;
            }
            $data['ordernum'] = addslashes($prefix.'_'.date('YmdHis'));
            $data['member_id'] = intval($_POST['member_id']);
            $data['product_type'] = $product_type;
            $data['payment'] = addslashes($_POST['payment']);
            $data['payment_type'] = 1;
            $data['postage'] = 0;
            if($product_type == 2 || $product_type == 3){
                $data['address_id'] = $_POST['address_id'] ? intval($_POST['address_id']) : $default_address->id;
                if(!$data['address_id']){
                    $_data = array('code'=>'3x000003','请添加收货地址');
                    response()->json($_data);
                }
            }else{
                $data['address_id'] = 0;
            }

            $data['pay_status'] = 10;
            $data['composition_status'] = 30;
            $data['add_date'] = date('Y-m-d H:i:s');
            $product_id = array();
            if(count($_POST['product_json']) == count($_POST['product_json'],1)){
                $product_id[] = $_POST['product_json']['pid'];
            }else{
                foreach($_POST['product_json'] as $ptm=>$pst){
                    $product_id[] = $pst['pid'];
                }
            }

            $orderlist = $order->getMemberOrderInfo(intval($_POST['member_id']),$product_id,$_POST['product_type']);
            if(!empty($orderlist) && $orderlist[0]->composition_status > 0){
                $_data = array('code'=>'3x000001','msg'=>'订单已存在，请勿重复下单');
            }else{
                $order_id = $order->insertOrder($data);
                if($order_id){
                    $product_list = $_POST['product_json'];
                    // $product_list = json_decode($product_json,1);
                    if(count($product_list) == count($product_list,1)){
                        $product['order_id'] = $order_id;
                        $product['product_id'] = $product_list['pid'];
                        $product['num'] = $product_list['pnum'];

                        $order->insertOrderProduct($product);
                    }else{
                        foreach($product_list as $ptm=>$pst){
                            $product['order_id'] = $order_id;
                            $product['product_id'] = $pst['pid'];
                            $product['num'] = $pst['pnum'];

                            $order->insertOrderProduct($product);
                        }
                    }


                    $_data = array('code'=>'2x000001','msg'=>'添加订单成功','orderid'=>$order_id,'userinfo'=>$userinfo);
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'订单添加失败');
                }
            }
        }
        return response()->json($_data);
    }

    public function view($id,$mid=0){
        if($mid == 0){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $order = new OrderService;
            $center = new CenterService;
            $common = new CommonService;
            $orderInfo = $order->getOrderInfo($id);
            /*if($orderInfo['address_id'] > 0){
                $orderInfo['addressList'][0] = $center->getAddressInfoById($orderInfo['address_id']);
                $provinceinfo = $common->getPcInfoById($orderInfo['addressList'][0]->province);
                if(!empty($provinceinfo)){
                    $orderInfo['addressList'][0]['province_name'] = $provinceinfo->name;
                }

                $cityinfo = $common->getPcInfoById($orderInfo['addressList'][0] ->city);
                if(!empty($cityinfo)){
                    $orderInfo['addressList'][0]['city_name'] = $cityinfo->name;
                }

                $countyinfo = $common->getPcInfoById($orderInfo['addressList'][0] ->county);
                if(!empty($countyinfo)){
                    $orderInfo['addressList'][0]['county_name'] = $countyinfo->name;
                }

            }else{*/
                $orderInfo['addressList'] = $center->getAddressList($orderInfo['member_id']);
                foreach($orderInfo['addressList'] as $oam=>$oat){
                    $provinceinfo = $common->getPcInfoById($oat->province);
                    if(!empty($provinceinfo)){
                        $orderInfo['addressList'][$oam]->province_name = $provinceinfo->name;
                    }

                    $cityinfo = $common->getPcInfoById($oat->city);
                    if(!empty($cityinfo)){
                        $orderInfo['addressList'][$oam]->city_name = $cityinfo->name;
                    }

                    $countyinfo = $common->getPcInfoById($oat->county);
                    if(!empty($countyinfo)){
                        $orderInfo['addressList'][$oam]->county_name = $countyinfo->name;
                    }

                }
            // }
            $_data = array('code'=>'2x000001','info'=>$orderInfo);
        }

        return response()->json($_data);
    }

    public function updateProductNum(){

        $order = new OrderService;
        $opId = intval($_POST['op_id']);
        $data['num'] = intval($_POST['pnum']);

        $res = $order->updateOrderProductNum($opId,$data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'产品数量修改成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'产品数量修改失败');
        }

        return response()->json($_data);
    }

    public function updateAddress(){

        date_default_timezone_set('PRC');
        $order = new OrderService;
        $orderid = intval($_POST['orderid']);
        $data['address_id'] = intval($_POST['address_id']);
        $data['update_date'] = date('Y-m-d H:i:s');
        $res = $order->updateOrder($orderid,$data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'订单地址修改成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'订单地址修改失败');
        }


        return response()->json($_data);
    }

    public function list($mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $order = new OrderService;
            $ordernum = $order->getOrderNum($mid);
            $orderlist = $order->getOrderList($ordernum,$mid);

            if(!empty($orderlist)){
                $_data = array('code'=>'2x000001','orderlist'=>$orderlist);
            }else{
                $_data = array('code'=>'4x000001','orderlist'=>array(),'msg'=>'暂无订单信息');
            }
        }

        return response()->json($_data);
    }
}